﻿using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;

namespace blackhouse.WebNemo.Web.Controls.ReferenceGallery {
    internal class GalleryController {

        #region Fields

        private string referenceColumnWithFileName;
        private string referenceColumnWithName;

        #endregion

        #region Properties

        public GroupRecord EditRecord { get; private set; }
        public IGroup EditRecordGroup { get; private set; }
        public IGroup ReferencingGroup { get; private set; }
        public GroupColumn EditRecordReferencingColumn { get; private set; }

        #endregion

        #region Constructors

        public GalleryController(GroupRecord editedRecord, IGroup editGroup, IGroup refGroup, int referenceColumnId)
            : this(editedRecord, editGroup, refGroup, editGroup.Columns[referenceColumnId]) { }

        public GalleryController(GroupRecord editedRecord, IGroup editGroup, IGroup refGroup, GroupColumn referenceColumn) {
            this.EditRecord = editedRecord;
            this.EditRecordGroup = editGroup;
            this.ReferencingGroup = refGroup;
            this.EditRecordReferencingColumn = referenceColumn;

            GroupColumn refColumnFile = this.ReferencingGroup.Columns.FirstOrDefault(c => c.ColumnType == FieldType.FileField);
            if (refColumnFile == null)
                throw new InvalidCastException("Grupa " + this.ReferencingGroup.Name + " nie zawiera żadnej kolumny typu plik!");
            this.referenceColumnWithFileName = refColumnFile.ColumnName;
            GroupColumn refColumnName = this.ReferencingGroup.Columns.FirstOrDefault(c => c.ColumnType == FieldType.ValueField && c.DataType == typeof(string));
            if (refColumnName != null)
                this.referenceColumnWithName = refColumnName.ColumnName;
        }

        #endregion

        #region Methods

        public GalleryItem[] GetGalleryPhotos(Size photoSize) {
            List<GroupRecord> records = new List<GroupRecord>();
            if (this.EditRecordReferencingColumn.ColumnType == FieldType.ManyReference) {
                records.AddRange(this.EditRecord.GetRecords(this.EditRecordReferencingColumn.ColumnName));
            }
            else {
                GroupRecord record = this.EditRecord.GetRecord(this.EditRecordReferencingColumn.ColumnName);
                if (record != null && record.Exists)
                    records.Add(record);
            }
            GroupRecord[] refRecords = records.OrderBy(gr => gr.LastModifiedTime).ToArray();

            GalleryItem[] photos = new GalleryItem[refRecords.Length];
            for (int xp = 0; xp < refRecords.Length; xp++)
                photos[xp] = GetGalleryItem(refRecords[xp], photoSize);

            return photos;
        }

        public GalleryItem UploadPhoto(HttpPostedFile postedFile, Size size) {
            GroupRecord newPhoto = new NewGroupRecord(this.ReferencingGroup);
            string filename = postedFile.FileName;
            string fileextension = filename.Substring(filename.LastIndexOf('.'));
            filename = filename.ReplaceEx(fileextension, "");
            newPhoto.SetFileValue(this.referenceColumnWithFileName, filename.CleanStringForUrl().RemovePolishChars().ToLower() + fileextension, postedFile.InputStream);
            if (!String.IsNullOrEmpty(this.referenceColumnWithName))
                newPhoto.SetValue(this.referenceColumnWithName, filename);
            newPhoto.Save();
            newPhoto = new GroupRecord(this.ReferencingGroup.Id, newPhoto.RecordId);
            return this.GetGalleryItem(newPhoto, size);
        }

        private GalleryItem GetGalleryItem(GroupRecord photoRecord, Size photoSize) {
            GalleryItem photo = new GalleryItem();
            if (!String.IsNullOrEmpty(this.referenceColumnWithName))
                photo.PhotoName = photoRecord.GetString(this.referenceColumnWithName);
            else
                photo.PhotoName = photoRecord.GetFileName(this.referenceColumnWithFileName);
            photo.PhotoSrc = photoRecord.GetImageUrl(this.referenceColumnWithFileName, photoSize);
            photo.RecordId = photoRecord.RecordId;
            return photo;
        }

        #endregion

    }
}